﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FizzBuzz.Service;

namespace FizzBuzz.ServiceImpl
{
    public class FizzBuzz : IFizzBuzz
    {
        public IInputValidator Validator { get; set; }
        public IRuleEngine RuleEngine { get; set; }
        public IOutput Output { get; set; }
        
        public void Run(int lowValue, int highValue)
        {
            var validationErrors = Validator.isValid(lowValue, highValue);
            if (validationErrors.Count() == 0)
            {
                var result = new StringBuilder();

                for (var i=lowValue;i<=highValue;i++){
                    result.AppendLine(
                        RuleEngine.ProcessRules(i)
                        );
                }
                Output.PrintLine(result.ToString());
            }
            else
            {
                Output.PrintLine(
                    validationErrors.Aggregate(
                        new StringBuilder(), 
                        (sb, x) => sb.AppendLine(x.DisplayError())
                    )
                    .ToString()
                    );
            }
        }
    }
}
